package com.itheima.Controller;

import com.itheima.Pojo.Department;
import com.itheima.Pojo.Result;
import com.itheima.Service.DepartmentService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/department")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @GetMapping
    public Result getAllDepartment() {
        log.info("getAllDepartment");
        List<Department> list = departmentService.getAllDepartment();
        return Result.success(list);
    }

    @DeleteMapping
    public Result deleteById(Integer code) {
        log.info("删除学院号为{}的学院", code);
        departmentService.deleteByCode(code);
        return Result.success();
    }

    @PutMapping
    public Result updateById(@RequestBody Department department) {
        log.info("修改学院号{}的班级", department.getCode());
        departmentService.updateByCode(department);
        return Result.success();
    }

    @PostMapping
    public Result addDepartment(@RequestBody Department department) {
        log.info("添加学院{}", department.getName());
        departmentService.addDepartment(department);
        return Result.success();
    }
}
